﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CrystalDecisions.Enterprise.Desktop;
using CrystalDecisions.Enterprise.Admin;

namespace BO.Management
{
    public static class ServerExtension
    {
        public static List<NameValueContainer> GetSpecificMetrics(this Server jobServer)
        {
            switch (jobServer.ServerKind)
            {
                case "jobserver":
                    return ServerExtension.GetProperties(new JobServerAdmin(jobServer.ServerAdmin));
                case "aps":
                    return ServerExtension.GetProperties(new ApsAdmin(jobServer.ServerAdmin));
                case "rptappserver":
                    return ServerExtension.GetProperties(new ReportAppServerAdmin(jobServer.ServerAdmin));
                case "fileserver":
                    return ServerExtension.GetProperties(new FileServerAdmin(jobServer.ServerAdmin));
                case "eventserver":
                    return ServerExtension.GetProperties(new EventServerAdmin(jobServer.ServerAdmin));
                case "pageserver":
                    return ServerExtension.GetProperties(new PageServerAdmin(jobServer.ServerAdmin));
                case "cacheserver":
                    return ServerExtension.GetProperties(new CacheServerAdmin(jobServer.ServerAdmin));
                default:
                    List < NameValueContainer > nometrics = new List<NameValueContainer>();
                    nometrics.Add(new NameValueContainer()
                        {
                            Name = String.Format("Server kind {0}",jobServer.ServerKind),
                            Value = "No metrics"
                        });
                    return nometrics;
            }

        }

        public class NameValueContainer
        {
            public String Name{get;set;}
            public String Value{get;set;}
        }

        internal static List<NameValueContainer> GetProperties(Object objectToDiscover)
        {
            List<NameValueContainer> listValues = new List<NameValueContainer>();
                System.ComponentModel.PropertyDescriptorCollection props = System.ComponentModel.TypeDescriptor.GetProperties(objectToDiscover);

                foreach (System.ComponentModel.PropertyDescriptor prop in props)
                {
                    Object value = null;

                    try
                    {
                        value = prop.GetValue(objectToDiscover);
                    }
                    catch (Exception ex)
                    {
                        value = ex.Message;
                    }

                    try
                    {
                        listValues.Add(new NameValueContainer()
                        {
                            Name=prop.Name,
                            Value = value.ToString()
                        });
                    }
                    catch (Exception ex)
                    {
                        listValues.Add(new NameValueContainer()
                        {
                            Name = prop.Name,
                            Value = ex.Message
                        });
                    }
                }

                return listValues;

        }

    }
}